package com.lecture.frame;

import com.lecture.utils.DrawLineUtil;
import com.lecture.utils.ViedioFileUtil;
import org.apache.commons.lang3.RandomStringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;

public class FrameWindow {
//    public static void main(String[] args) {
//        createWindow();
//    }
    /**
     * 创建主窗口
     */
    public static void createWindow(){
        //创建JFrame实例
        JFrame frame = new JFrame("讲课插件DIY");
        //设置窗口的宽高
        frame.setSize(450,300);
        //设置窗口退出方式
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //禁用最大化窗口
        frame.setResizable(false);
        URL resource = FrameWindow.class.getResource("/dog.png");
        //去除JFRAM的java图标
        Image icon = Toolkit.getDefaultToolkit().getImage(resource);
        frame.setIconImage(icon);
        //设置窗口弹出的路径
        int windowWidth = frame.getWidth(); //获得窗口宽
        int windowHeight = frame.getHeight(); //获得窗口高
        Toolkit kit = Toolkit.getDefaultToolkit(); //定义工具包
        Dimension screenSize = kit.getScreenSize(); //获取屏幕的尺寸
        int screenWidth = screenSize.width; //获取屏幕的宽
        int screenHeight = screenSize.height; //获取屏幕的高
        frame.setLocation(screenWidth/2-windowWidth/2, screenHeight/2-windowHeight/2);//设置窗口居中显示
        //创建选项卡
        JTabbedPane tab = new JTabbedPane();
        frame.add(tab);
        /**
         * 创建面板.类似HTML中div
         * 我们可以创建多个面板并在 JFrame 中指定位置
         *  面板中我们可以添加文本字段，按钮及其他组件。
         */
        JPanel imagePanel = new JPanel();
        JPanel videoPanel = new JPanel();
        createFilePathComp(imagePanel);
        createVideoComp(videoPanel);
        //添加到选项卡
        tab.add("图片边框处理",imagePanel);
        tab.add("视频信息处理",videoPanel);

        //设置可见,放到最后
        frame.setVisible(true);

    }

    /**
     * 图片边框处理
     * @param jPanel
     */
    private static void createFilePathComp(JPanel jPanel){
        JLabel label = new JLabel("图片目录选择");
        label.setBounds(10,20,80,25);
        jPanel.add(label);
        /*
         * 创建文本域用于用户输入
         */
        final JTextField imageDir = new JTextField(20);
        imageDir.setBounds(100,20,165,25);
        jPanel.add(imageDir);
        /*
            创建button
         */
        JButton selectPath=new JButton("选择路径");
        jPanel.add(selectPath);
        selectPath.setBounds(10,50,80,25);
        //
        selectPath.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc=new JFileChooser();
                jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
                jfc.showDialog(new JLabel(), "选择");
                File file=jfc.getSelectedFile();
                if(file.isDirectory()){
                    //将路径值给text属性
                    imageDir.setText(file.getAbsolutePath());
                    System.out.println("dirPath"+file.getAbsolutePath());

                }else if(file.isFile()){
                    imageDir.setText(file.getAbsolutePath());
                    System.out.println("filePath"+file.getAbsolutePath());
                }
            }
        });
        JButton drawButton = new JButton("边框生成");
        jPanel.add(drawButton);
        drawButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String path = imageDir.getText();
                if(path!=null && !path.trim().equals("")){
                    System.out.println("start draw line......");
                    int fileCount = DrawLineUtil.processImgs(path);
                    JOptionPane.showMessageDialog(null,"处理了:"+fileCount+"图片!");
                    System.out.println("process file count:"+fileCount);
                }else{
                    JOptionPane.showMessageDialog(null,"请选择正确路径");
                }
            }
        });
    }


    /**
     * 视频信息导出
     * @param jPanel
     */
    private static void createVideoComp(JPanel jPanel){
        JLabel label = new JLabel("视频目录选择");
        label.setBounds(10,20,80,25);
        jPanel.add(label, BorderLayout.WEST);

        /*
         * 创建文本域用于用户输入
         */
        final JTextField viedioDir = new JTextField(20);
        viedioDir.setBounds(100,20,165,25);
        jPanel.add(viedioDir,BorderLayout.CENTER);

        /*
            创建button
         */
        JButton selectPath=new JButton("选择路径");
        jPanel.add(selectPath, BorderLayout.EAST);
        selectPath.setBounds(10,50,80,25);


        //监听
        selectPath.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc=new JFileChooser();
                jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
                jfc.showDialog(new JLabel(), "选择");
                File file=jfc.getSelectedFile();
                if(file.isDirectory()){
                    //将路径值给text属性
                    viedioDir.setText(file.getAbsolutePath());
                    System.out.println("dirPath"+file.getAbsolutePath());

                }else if(file.isFile()){
                    viedioDir.setText(file.getAbsolutePath());
                    System.out.println("filePath"+file.getAbsolutePath());
                }
            }
        });


        final JLabel exportLabel = new JLabel("Excel导出路径");
        exportLabel.setBounds(10,80,80,25);
        jPanel.add(exportLabel,BorderLayout.WEST);

        final JTextField excelDir = new JTextField(20);
        excelDir.setBounds(100,50,165,25);
        jPanel.add(excelDir,BorderLayout.CENTER);

        JButton viedioButton=new JButton("选择路径");
        jPanel.add(viedioButton,BorderLayout.WEST);
        viedioButton.setBounds(10,50,80,25);

        //监听
        viedioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc=new JFileChooser();
                jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
                jfc.showDialog(new JLabel(), "选择");
                File file=jfc.getSelectedFile();
                if(file.isDirectory()){
                    //将路径值给text属性
                    String fileName = RandomStringUtils.randomNumeric(4);
                    excelDir.setText(file.getAbsolutePath()+"\\"+fileName+".xls");
                    System.out.println("dirPath"+file.getAbsolutePath());

                }else if(file.isFile()){
                    excelDir.setText(file.getAbsolutePath());
                    System.out.println("filePath"+file.getAbsolutePath());
                }
            }
        });
        JButton vButton = new JButton("导出视频信息");
        jPanel.add(vButton,BorderLayout.CENTER);
        vButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String path = excelDir.getText();
                String path2=viedioDir.getText();
                System.out.println(path);
                System.out.println(path2);
                //导出视频信息
                int count = ViedioFileUtil.processViedio(viedioDir.getText(), excelDir.getText());
                JOptionPane.showMessageDialog(null,"共导出:"+count+"个视频信息!");

            }
        });
    }



}
